package device

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log"
)

type DeviceApi struct{}

// CreateDevice 创建设备信息
// @Tags Device
// @Summary 创建设备信息
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body device.Device true "创建设备信息"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /Dev/createDevice [post]
func (DevApi *DeviceApi) CreateDevice(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var Dev device.Device
	err := c.ShouldBindJSON(&Dev)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = DevService.CreateDevice(ctx, &Dev)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteDevice 删除设备信息
// @Tags Device
// @Summary 删除设备信息
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body device.Device true "删除设备信息"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /Dev/deleteDevice [delete]
func (DevApi *DeviceApi) DeleteDevice(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := DevService.DeleteDevice(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteDeviceByIds 批量删除设备信息
// @Tags Device
// @Summary 批量删除设备信息
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /Dev/deleteDeviceByIds [delete]
func (DevApi *DeviceApi) DeleteDeviceByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := DevService.DeleteDeviceByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateDevice 更新设备信息
// @Tags Device
// @Summary 更新设备信息
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body device.Device true "更新设备信息"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /Dev/updateDevice [put]
func (DevApi *DeviceApi) UpdateDevice(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var Dev device.Device
	err := c.ShouldBindJSON(&Dev)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = DevService.UpdateDevice(ctx, Dev)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindDevice 用id查询设备信息
// @Tags Device
// @Summary 用id查询设备信息
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询设备信息"
// @Success 200 {object} response.Response{data=device.Device,msg=string} "查询成功"
// @Router /Dev/findDevice [get]
func (DevApi *DeviceApi) FindDevice(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	reDev, err := DevService.GetDevice(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reDev, c)
}

// GetDeviceList 分页获取设备信息列表
// @Tags Device
// @Summary 分页获取设备信息列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query deviceReq.DeviceSearch true "分页获取设备信息列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /Dev/getDeviceList [get]
func (DevApi *DeviceApi) GetDeviceList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo deviceReq.DeviceSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := DevService.GetDeviceInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetDevicePublic 不需要鉴权的设备信息接口
// @Tags Device
// @Summary 不需要鉴权的设备信息接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /Dev/getDevicePublic [get]
func (DevApi *DeviceApi) GetDevicePublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	DevService.GetDevicePublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的设备信息接口信息",
	}, "获取成功", c)
}

// DeviceDashboard 设备看板
// @Tags Device
// @Summary 设备看板
// @Accept application/json
// @Produce application/json
// @Param data query deviceReq.DeviceSearch true "成功"
// @Success 200 {object} response.Response{data=object,msg=string} "成功"
// @Router /Dev/deviceDashboard [GET]
func (DevApi *DeviceApi) DeviceDashboard(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()
	// 请添加自己的业务逻辑
	data, err := DevService.DeviceDashboard(ctx)
	if err != nil {
		global.GVA_LOG.Error("失败!", zap.Error(err))
		response.FailWithMessage("失败", c)
		return
	}
	log.Println(c)
	response.OkWithData(data, c)
}
